
from django.db import models
from django.utils import timezone

# ==========================================
# Core Models
# ==========================================

class Application(models.Model):
    name = models.CharField(max_length=50, verbose_name='应用名称')
    icon = models.CharField(max_length=100, verbose_name='图标类名')
    color = models.CharField(max_length=20, verbose_name='颜色')
    url = models.CharField(max_length=200, blank=True, verbose_name='跳转链接')
    description = models.TextField(blank=True, verbose_name='应用描述')
    APP_TYPE_CHOICES = [
        ('business', '业务应用'),
        ('administrative', '行政应用'),
        ('operations', '运营应用'),
        ('configuration', '配置应用'),
        ('visualization', '可视化应用'),
    ]
    app_type = models.CharField(max_length=20, choices=APP_TYPE_CHOICES, default='business', verbose_name='应用类型')
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    sort_order = models.IntegerField(default=0, verbose_name='排序')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')

    class Meta:
        verbose_name = '应用'
        verbose_name_plural = '应用'
        db_table = '应用表'
        ordering = ['sort_order', 'id']

    def __str__(self):
        return self.name

class Department(models.Model):
    dept_id = models.BigAutoField(primary_key=True, db_column='dept_id', verbose_name='部门ID')
    parent_id = models.BigIntegerField(default=0, verbose_name='父部门ID')
    ancestors = models.CharField(max_length=50, default='', verbose_name='祖级列表')
    dept_name = models.CharField(max_length=30, default='', verbose_name='部门名称')
    order_num = models.IntegerField(default=0, verbose_name='显示顺序')
    leader = models.CharField(max_length=20, null=True, verbose_name='负责人')
    phone = models.CharField(max_length=11, null=True, verbose_name='联系电话')
    email = models.CharField(max_length=50, null=True, verbose_name='邮箱')
    status = models.CharField(max_length=1, default='0', verbose_name='部门状态')
    del_flag = models.CharField(max_length=1, default='0', verbose_name='删除标志')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, null=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '部门表'
        verbose_name_plural = '部门表'
        db_table = '部门表'

class Permission_group(models.Model):
    PERMIT_CHOICES = [
        (1, '管理组'),
        (2, '规划设计中心许可组'),
        (3, '商务服务组'),
        (4, '数据勘测中心许可组'),
        (5, '资源评价中心许可组'),
        (6, '评估咨询一中心许可组'),
        (7, '评估咨询二中心许可组'),
        (8, '经纪代理中心许可组'),
        (9, '江阴事业中心许可组'),
        (10, '南京事业中心许可组'),
        (11, '普通用户'),
        (12, '离职'),
    ]
    permit = models.SmallIntegerField(choices=PERMIT_CHOICES, default=11, unique=True, verbose_name='授权组')

    class Meta:
        verbose_name = '授权组'
        verbose_name_plural = '授权组'
        db_table = '授权组表'

class User(models.Model):
    id = models.BigAutoField(primary_key=True, db_column='user_id', verbose_name='用户ID')
    zh_name = models.CharField(max_length=30, default='', db_column='user_name', verbose_name='用户账号')
    nick_name = models.CharField(max_length=30, default='', verbose_name='用户昵称')
    user_type = models.CharField(max_length=2, default='00', verbose_name='用户类型')
    email = models.CharField(max_length=50, default='', verbose_name='用户邮箱')
    phonenumber = models.CharField(max_length=11, default='', verbose_name='手机号码')
    sex = models.CharField(max_length=1, default='0', verbose_name='用户性别')
    wx_avatar_url = models.CharField(max_length=100, default='', db_column='avatar', verbose_name='头像地址')
    zh_pwd = models.CharField(max_length=100, default='', db_column='password', verbose_name='密码')
    status = models.CharField(max_length=1, default='0', verbose_name='账号状态')
    del_flag = models.CharField(max_length=1, default='0', verbose_name='删除标志')
    login_ip = models.CharField(max_length=128, default='', verbose_name='最后登录IP')
    login_date = models.DateTimeField(null=True, verbose_name='最后登录时间')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, null=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')
    open_id = models.CharField(max_length=100, blank=True, null=True, verbose_name='微信OpenID')
    part_id = models.ForeignKey(Department, on_delete=models.SET_NULL, null=True, db_column='dept_id', verbose_name='部门')
    per_team = models.CharField(max_length=50, blank=True, verbose_name='工作组')
    Per_group = models.ForeignKey(Permission_group, on_delete=models.SET_NULL, null=True, verbose_name='权限组')
    is_active_bool = models.BooleanField(default=True, verbose_name='是否激活')

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'
        db_table = '用户表'

class UserApplicationPermission(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    application = models.ForeignKey(Application, on_delete=models.CASCADE, verbose_name='应用')
    department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='部门')
    permission_group = models.ForeignKey(Permission_group, on_delete=models.CASCADE, verbose_name='权限组')
    is_granted = models.BooleanField(default=True, verbose_name='是否授权')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')

    class Meta:
        verbose_name = '用户应用权限'
        verbose_name_plural = '用户应用权限'
        db_table = '用户应用权限表'
        unique_together = ('user', 'application')

class RoleApplicationPermission(models.Model):
    permission_group = models.ForeignKey(Permission_group, on_delete=models.CASCADE, verbose_name='权限组')
    department = models.ForeignKey(Department, on_delete=models.CASCADE, blank=True, null=True, verbose_name='部门')
    application = models.ForeignKey(Application, on_delete=models.CASCADE, verbose_name='应用')
    is_granted = models.BooleanField(default=True, verbose_name='是否授权')
    create_time = models.DateTimeField(default=timezone.now, verbose_name='创建时间')
    is_disabled = models.BooleanField(default=False, verbose_name='是否禁用')

    class Meta:
        verbose_name = '角色应用权限'
        verbose_name_plural = '角色应用权限'
        db_table = '角色应用权限表'
        unique_together = ('permission_group', 'department', 'application')

# ==========================================
# New System Models (SYS)
# ==========================================

class SysRole(models.Model):
    role_id = models.BigAutoField(primary_key=True, verbose_name='角色ID')
    role_name = models.CharField(max_length=30, verbose_name='角色名称')
    role_key = models.CharField(max_length=100, verbose_name='角色权限字符串')
    role_sort = models.IntegerField(verbose_name='显示顺序')
    data_scope = models.CharField(max_length=1, default='1', verbose_name='数据范围')
    menu_check_strictly = models.BooleanField(default=True, verbose_name='菜单树选择项是否关联显示')
    dept_check_strictly = models.BooleanField(default=True, verbose_name='部门树选择项是否关联显示')
    status = models.CharField(max_length=1, verbose_name='角色状态')
    del_flag = models.CharField(max_length=1, default='0', verbose_name='删除标志')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')

    class Meta:
        db_table = '角色表'
        verbose_name = '角色信息'


class SysMenu(models.Model):
    menu_id = models.BigAutoField(primary_key=True, verbose_name='菜单ID')
    menu_name = models.CharField(max_length=50, verbose_name='菜单名称')
    parent_id = models.BigIntegerField(default=0, verbose_name='父菜单ID')
    order_num = models.IntegerField(default=0, verbose_name='显示顺序')
    path = models.CharField(max_length=200, default='', verbose_name='路由地址')
    component = models.CharField(max_length=255, null=True, verbose_name='组件路径')
    query = models.CharField(max_length=255, null=True, verbose_name='路由参数')
    route_name = models.CharField(max_length=50, default='', verbose_name='路由名称')
    is_frame = models.IntegerField(default=1, verbose_name='是否为外链')
    is_cache = models.IntegerField(default=0, verbose_name='是否缓存')
    menu_type = models.CharField(max_length=1, default='', verbose_name='菜单类型')
    visible = models.CharField(max_length=1, default='0', verbose_name='菜单状态')
    status = models.CharField(max_length=1, default='0', verbose_name='菜单状态')
    perms = models.CharField(max_length=100, null=True, verbose_name='权限标识')
    icon = models.CharField(max_length=100, default='#', verbose_name='菜单图标')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, default='', verbose_name='备注')

    class Meta:
        db_table = '菜单表'
        verbose_name = '菜单权限'


class SysDictType(models.Model):
    dict_id = models.BigAutoField(primary_key=True, verbose_name='字典主键')
    dict_name = models.CharField(max_length=100, default='', verbose_name='字典名称')
    dict_type = models.CharField(max_length=100, default='', unique=True, verbose_name='字典类型')
    status = models.CharField(max_length=1, default='0', verbose_name='状态')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')

    class Meta:
        db_table = '字典类型表'
        verbose_name = '字典类型'


class SysDictData(models.Model):
    dict_code = models.BigAutoField(primary_key=True, verbose_name='字典编码')
    dict_sort = models.IntegerField(default=0, verbose_name='字典排序')
    dict_label = models.CharField(max_length=100, default='', verbose_name='字典标签')
    dict_value = models.CharField(max_length=100, default='', verbose_name='字典键值')
    dict_type = models.CharField(max_length=100, default='', verbose_name='字典类型')
    css_class = models.CharField(max_length=100, null=True, verbose_name='样式属性')
    list_class = models.CharField(max_length=100, null=True, verbose_name='表格回显样式')
    is_default = models.CharField(max_length=1, default='N', verbose_name='是否默认')
    status = models.CharField(max_length=1, default='0', verbose_name='状态')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')

    class Meta:
        db_table = '字典数据表'
        verbose_name = '字典数据'


class SysConfig(models.Model):
    config_id = models.AutoField(primary_key=True, verbose_name='参数主键')
    config_name = models.CharField(max_length=100, default='', verbose_name='参数名称')
    config_key = models.CharField(max_length=100, default='', verbose_name='参数键名')
    config_value = models.CharField(max_length=500, default='', verbose_name='参数键值')
    config_type = models.CharField(max_length=1, default='N', verbose_name='系统内置')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')

    class Meta:
        db_table = '参数配置表'
        verbose_name = '参数配置'


class SysJob(models.Model):
    job_id = models.BigAutoField(primary_key=True, verbose_name='任务ID')
    job_name = models.CharField(max_length=64, default='', verbose_name='任务名称')
    job_group = models.CharField(max_length=64, default='DEFAULT', verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    cron_expression = models.CharField(max_length=255, default='', verbose_name='cron执行表达式')
    misfire_policy = models.CharField(max_length=20, default='3', verbose_name='计划执行错误策略')
    concurrent = models.CharField(max_length=1, default='1', verbose_name='是否并发执行')
    status = models.CharField(max_length=1, default='0', verbose_name='状态')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(null=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, default='', verbose_name='备注信息')

    class Meta:
        db_table = '定时任务调度表'
        verbose_name = '定时任务'


class SysJobLog(models.Model):
    job_log_id = models.BigAutoField(primary_key=True, verbose_name='任务日志ID')
    job_name = models.CharField(max_length=64, verbose_name='任务名称')
    job_group = models.CharField(max_length=64, verbose_name='任务组名')
    invoke_target = models.CharField(max_length=500, verbose_name='调用目标字符串')
    job_message = models.CharField(max_length=500, null=True, verbose_name='日志信息')
    status = models.CharField(max_length=1, default='0', verbose_name='执行状态')
    exception_info = models.CharField(max_length=2000, default='', verbose_name='异常信息')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')

    class Meta:
        db_table = '定时任务调度日志表'
        verbose_name = '定时任务日志'


class SysLogininfor(models.Model):
    info_id = models.BigAutoField(primary_key=True, verbose_name='访问ID')
    user_name = models.CharField(max_length=50, default='', verbose_name='用户账号')
    ipaddr = models.CharField(max_length=128, default='', verbose_name='登录IP地址')
    login_location = models.CharField(max_length=255, default='', verbose_name='登录地点')
    browser = models.CharField(max_length=50, default='', verbose_name='浏览器类型')
    os = models.CharField(max_length=50, default='', verbose_name='操作系统')
    status = models.CharField(max_length=1, default='0', verbose_name='登录状态')
    msg = models.CharField(max_length=255, default='', verbose_name='提示消息')
    login_time = models.DateTimeField(null=True, verbose_name='访问时间')

    class Meta:
        db_table = '系统访问记录'
        verbose_name = '系统访问记录'


class SysNotice(models.Model):
    notice_id = models.AutoField(primary_key=True, verbose_name='公告ID')
    notice_title = models.CharField(max_length=50, verbose_name='公告标题')
    notice_type = models.CharField(max_length=1, verbose_name='公告类型')
    notice_content = models.TextField(null=True, verbose_name='公告内容')
    status = models.CharField(max_length=1, default='0', verbose_name='公告状态')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(null=True, verbose_name='更新时间')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')

    class Meta:
        db_table = '通知公告表'
        verbose_name = '通知公告'


class SysOperLog(models.Model):
    oper_id = models.BigAutoField(primary_key=True, verbose_name='日志主键')
    title = models.CharField(max_length=50, default='', verbose_name='模块标题')
    business_type = models.IntegerField(default=0, verbose_name='业务类型')
    method = models.CharField(max_length=200, default='', verbose_name='方法名称')
    request_method = models.CharField(max_length=10, default='', verbose_name='请求方式')
    operator_type = models.IntegerField(default=0, verbose_name='操作类别')
    oper_name = models.CharField(max_length=50, default='', verbose_name='操作人员')
    dept_name = models.CharField(max_length=50, default='', verbose_name='部门名称')
    oper_url = models.CharField(max_length=255, default='', verbose_name='请求URL')
    oper_ip = models.CharField(max_length=128, default='', verbose_name='主机地址')
    oper_location = models.CharField(max_length=255, default='', verbose_name='操作地点')
    oper_param = models.CharField(max_length=2000, default='', verbose_name='请求参数')
    json_result = models.CharField(max_length=2000, default='', verbose_name='返回参数')
    status = models.IntegerField(default=0, verbose_name='操作状态')
    error_msg = models.CharField(max_length=2000, default='', verbose_name='错误消息')
    oper_time = models.DateTimeField(null=True, verbose_name='操作时间')
    cost_time = models.BigIntegerField(default=0, verbose_name='消耗时间')

    class Meta:
        db_table = '操作日志记录'
        verbose_name = '操作日志记录'



class SysPost(models.Model):
    post_id = models.BigAutoField(primary_key=True, verbose_name='岗位ID')
    post_code = models.CharField(max_length=64, verbose_name='岗位编码')
    post_name = models.CharField(max_length=50, verbose_name='岗位名称')
    post_sort = models.IntegerField(verbose_name='显示顺序')
    status = models.CharField(max_length=1, verbose_name='状态')
    create_by = models.CharField(max_length=64, default='', verbose_name='创建者')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')
    update_by = models.CharField(max_length=64, default='', verbose_name='更新者')
    update_time = models.DateTimeField(null=True, verbose_name='更新时间')
    remark = models.CharField(max_length=500, null=True, verbose_name='备注')

    class Meta:
        db_table = '岗位信息表'
        verbose_name = '岗位信息'


class SysRoleDept(models.Model):
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, db_column='role_id')
    dept = models.ForeignKey(Department, on_delete=models.CASCADE, db_column='dept_id')
    class Meta:
        db_table = '角色和部门关联表'
        verbose_name = '角色和部门关联'
        verbose_name_plural = '角色和部门关联'
        unique_together = (('role', 'dept'),)


class SysRoleMenu(models.Model):
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, db_column='role_id')
    menu = models.ForeignKey(SysMenu, on_delete=models.CASCADE, db_column='menu_id')
    class Meta:
        db_table = '角色和菜单关联表'
        verbose_name = '角色和菜单关联'
        verbose_name_plural = '角色和菜单关联'
        unique_together = (('role', 'menu'),)


class SysUserPost(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, db_column='user_id')
    post = models.ForeignKey(SysPost, on_delete=models.CASCADE, db_column='post_id')
    class Meta:
        db_table = '用户和岗位关联表'
        verbose_name = '用户和岗位关联'
        verbose_name_plural = '用户和岗位关联'
        unique_together = (('user', 'post'),)


class SysUserRole(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, db_column='user_id')
    role = models.ForeignKey(SysRole, on_delete=models.CASCADE, db_column='role_id')
    class Meta:
        db_table = '用户和角色关联表'
        verbose_name = '用户和角色关联'
        verbose_name_plural = '用户和角色关联'
        unique_together = (('user', 'role'),)


# ==========================================
# CRM Models
# ==========================================

class CrmAppraisalProject(models.Model):
    id = models.BigAutoField(primary_key=True)
    code_head = models.CharField(max_length=20, null=True, verbose_name='编号简称')
    dept_id = models.BigIntegerField(null=True, verbose_name='发起部门')
    is_single = models.CharField(max_length=2, null=True, verbose_name='是否单一项目')
    project_owner_id = models.CharField(max_length=255, null=True, verbose_name='项目归属')
    report_number = models.CharField(max_length=50, null=True, verbose_name='报告编号')
    type = models.CharField(max_length=4, null=True, verbose_name='项目类型')
    name = models.CharField(max_length=255, null=True, verbose_name='项目名称')
    address = models.CharField(max_length=255, null=True, verbose_name='坐落')
    appraisal_purpose = models.CharField(max_length=255, null=True, verbose_name='估价目的')
    area = models.CharField(max_length=255, null=True, verbose_name='所属区域')
    principal = models.CharField(max_length=255, null=True, verbose_name='委托方')
    principal_user = models.CharField(max_length=20, null=True, verbose_name='委托方联系人')
    principal_phone = models.CharField(max_length=20, null=True, verbose_name='委托方联系电话')
    principal_address = models.CharField(max_length=255, null=True, verbose_name='委托方联系地址')
    source_id = models.BigIntegerField(null=True, verbose_name='业务来源')
    source_user = models.CharField(max_length=255, null=True, verbose_name='业务来源联系人')
    source_phone = models.CharField(max_length=20, null=True, verbose_name='业务来源联系电话')
    cooperative_company = models.CharField(max_length=255, null=True, verbose_name='合作单位')
    owner_user = models.BigIntegerField(null=True, verbose_name='项目负责人')
    appraisal_user = models.BigIntegerField(null=True, verbose_name='评估人员')
    sale_user = models.BigIntegerField(null=True, verbose_name='商务负责人')
    sale_support_user = models.BigIntegerField(null=True, verbose_name='商务维护人')
    house_usage = models.CharField(max_length=4, null=True, verbose_name='房产用途')
    house_area = models.FloatField(null=True, verbose_name='房产面积')
    land_usage = models.CharField(max_length=4, null=True, verbose_name='土地用途')
    land_area = models.FloatField(null=True, verbose_name='土地面积')
    seller = models.CharField(max_length=50, null=True, verbose_name='出卖人')
    assignee = models.CharField(max_length=50, null=True, verbose_name='受让人')
    trade_type = models.CharField(max_length=255, null=True, verbose_name='交易类型')
    land_usage_right_type = models.CharField(max_length=255, null=True, verbose_name='土地使用权类型')
    estimated_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='预计收费金额')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    commit_survey_time = models.DateTimeField(null=True, verbose_name='提交勘察时间')
    survey_user = models.BigIntegerField(null=True, verbose_name='现场勘察人')
    survey_time = models.DateTimeField(null=True, verbose_name='现场勘察日期')
    filing_date = models.DateField(null=True, verbose_name='归档日期')
    filing_user = models.BigIntegerField(null=True, verbose_name='归档人员')
    filing_opinion = models.CharField(max_length=255, null=True, verbose_name='归档意见')
    deactivation_date = models.DateField(null=True, verbose_name='注销日期')
    deactivation_reason = models.CharField(max_length=255, null=True, verbose_name='注销原因')
    stamp_date = models.DateField(null=True, verbose_name='盖章完成日期')
    stamp_user = models.BigIntegerField(null=True, verbose_name='盖章人员')
    stamp_report_count = models.CharField(max_length=255, null=True, verbose_name='盖章报告份数')
    filing_commit_date = models.DateField(null=True, verbose_name='提交归档日期')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '估价项目表'
        verbose_name = '估价项目'


class CrmBidding(models.Model):
    id = models.BigAutoField(primary_key=True)
    dept_id = models.BigIntegerField(null=True, verbose_name='发起部门')
    affiliation_module = models.CharField(max_length=4, null=True, verbose_name='所属模块')
    code = models.CharField(max_length=20, null=True, verbose_name='招投标编号')
    is_normal = models.CharField(max_length=2, null=True, verbose_name='是否常规项目')
    project_code = models.CharField(max_length=30, null=True, verbose_name='项目编号')
    project_name = models.CharField(max_length=50, null=True, verbose_name='项目名称')
    owner_id = models.BigIntegerField(null=True, verbose_name='招投标负责人')
    fee_type = models.CharField(max_length=2, null=True, verbose_name='招投标费用类型')
    sale_id = models.BigIntegerField(null=True, verbose_name='商务负责人')
    principal = models.CharField(max_length=255, null=True, verbose_name='委托方')
    principal_user = models.CharField(max_length=20, null=True, verbose_name='委托方联系人')
    principal_phone = models.CharField(max_length=20, null=True, verbose_name='委托方联系电话')
    agency = models.CharField(max_length=50, null=True, verbose_name='代理机构')
    agency_user = models.CharField(max_length=20, null=True, verbose_name='代理联系人')
    agency_phone = models.CharField(max_length=255, null=True, verbose_name='代理联系电话')
    bidding_type = models.CharField(max_length=4, null=True, verbose_name='招投标方式')
    bidding_company = models.CharField(max_length=50, null=True, verbose_name='投标单位')
    max_limited_price = models.DecimalField(max_digits=10, decimal_places=2, null=True, verbose_name='最高限价')
    intention_dead_date = models.DateField(null=True, verbose_name='采购意向公告截至时间')
    time_remark = models.CharField(max_length=255, null=True, verbose_name='招投标工作时间要求')
    remark = models.CharField(max_length=255, null=True, verbose_name='招投标备注')
    bid_open_time = models.DateTimeField(null=True, verbose_name='开标时间')
    bid_win_date = models.DateField(null=True, verbose_name='中标日期')
    deactivation_date = models.DateField(null=True, verbose_name='注销日期')
    deactivation_reason = models.CharField(max_length=255, null=True, verbose_name='注销原因')
    filing_date = models.DateField(null=True, verbose_name='归档日期')
    filing_number = models.CharField(max_length=255, null=True, verbose_name='档案号')
    stamp_date = models.DateField(null=True, verbose_name='盖章日期')
    issue_pass_date = models.DateField(null=True, verbose_name='签发通过日期')
    bid_win_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='中标金额')
    bid_win_result = models.CharField(max_length=255, null=True, verbose_name='中标结果')
    bid_quotation = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='投标报价')
    writer = models.BigIntegerField(null=True, verbose_name='撰写人员')
    assistant_writer = models.CharField(max_length=255, null=True, verbose_name='辅助撰写人')
    open_time = models.DateTimeField(null=True, verbose_name='开标时间')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '招投标基础信息表'
        verbose_name = '招投标基础信息'


class CrmBiddingProject(models.Model):
    id = models.BigAutoField(primary_key=True)
    bidding_id = models.BigIntegerField(null=True, verbose_name='招投标id')
    project_id = models.BigIntegerField(null=True, verbose_name='项目id')
    affiliation_module = models.CharField(max_length=4, null=True, verbose_name='所属模块')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '招投标与项目关系表'
        verbose_name = '招投标与项目关系'


class CrmChance(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='商机id')
    customer_id = models.BigIntegerField(null=True, verbose_name='客户id')
    number = models.CharField(max_length=255, null=True, verbose_name='商机编号')
    name = models.CharField(max_length=50, null=True, verbose_name='商机名称')
    chance_source = models.CharField(max_length=1, default='4', verbose_name='商机来源')
    sale_id = models.BigIntegerField(null=True, verbose_name='销售负责人id')
    dept_id = models.BigIntegerField(null=True, verbose_name='销售负责人部门')
    signup_date = models.DateTimeField(verbose_name='预计签约日期')
    signup_money = models.DecimalField(max_digits=30, decimal_places=6, default=0, verbose_name='预计签单金额')
    address = models.CharField(max_length=255, null=True, verbose_name='地址')
    signup_probability = models.IntegerField(default=25, verbose_name='成单率')
    remark = models.CharField(max_length=255, null=True, verbose_name='描述')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '商机信息表'
        verbose_name = '商机信息'


class CrmChanceFollow(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='跟进id')
    chance_id = models.BigIntegerField(verbose_name='商机id')
    contact_person = models.CharField(max_length=200, null=True, verbose_name='客户联系人')
    signup_probability = models.IntegerField(default=0, null=True, verbose_name='成单率')
    sales_stage = models.CharField(max_length=1, null=True, verbose_name='销售阶段')
    follow_mode = models.CharField(max_length=1, verbose_name='跟进方式')
    follow_content = models.CharField(max_length=255, default='', verbose_name='跟进内容')
    follow_date = models.DateTimeField(verbose_name='跟进时间')
    follow_date_next = models.DateTimeField(null=True, verbose_name='下次跟进时间')
    follow_remind_on_off = models.CharField(max_length=2, default='00', verbose_name='跟进提醒开关')
    user_id = models.IntegerField(null=True, verbose_name='跟进人id')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    process_key = models.CharField(max_length=255, default='process-chance-follow', verbose_name='业务key')
    signup_money = models.DecimalField(max_digits=30, decimal_places=6, null=True, verbose_name='预计签约金额')
    create_user_id = models.CharField(max_length=200, null=True, verbose_name='创建人')
    create_time = models.DateTimeField(null=True, verbose_name='创建时间')
    update_user_id = models.CharField(max_length=200, null=True, verbose_name='修改人')
    update_time = models.DateTimeField(auto_now=True, null=True, verbose_name='修改时间')
    del_flag = models.IntegerField(default=0, null=True, verbose_name='删除标志')

    class Meta:
        db_table = '商机跟进表'
        verbose_name = '商机跟进'


class CrmCustomer(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='客户id')
    dept_id = models.BigIntegerField(null=True, verbose_name='分公司id')
    sale_id = models.BigIntegerField(null=True, verbose_name='销售负责人')
    status = models.CharField(max_length=2, default='01', verbose_name='状态')
    customer_name = models.CharField(max_length=50, null=True, verbose_name='客户名称')
    customer_classification = models.CharField(max_length=255, null=True, verbose_name='客户分类')
    contact_address = models.CharField(max_length=255, null=True, verbose_name='客户联系地址')
    customer_level = models.CharField(max_length=4, null=True, verbose_name='客户等级')
    invoice_title = models.CharField(max_length=255, null=True, verbose_name='发票抬头')
    ident_number = models.CharField(max_length=255, null=True, verbose_name='纳税人识别号')
    deposit_bank = models.CharField(max_length=255, null=True, verbose_name='开户银行')
    bank_account = models.CharField(max_length=255, null=True, verbose_name='银行账号')
    contact_phone = models.CharField(max_length=255, null=True, verbose_name='地址电话')
    remark = models.CharField(max_length=255, null=True, verbose_name='客户备注')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, null=True, verbose_name='删除标志')

    class Meta:
        db_table = '客户信息表'
        verbose_name = '客户信息'


class CrmCustomerCandidate(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='客户入围id')
    customer_id = models.BigIntegerField(null=True, verbose_name='客户ID')
    candidate_item = models.CharField(max_length=255, null=True, verbose_name='入围事项')
    candidate_date = models.DateField(null=True, verbose_name='入围日期')
    overdue_date = models.DateField(null=True, verbose_name='到期日期')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, null=True, verbose_name='删除标志')

    class Meta:
        db_table = '客户入围表'
        verbose_name = '客户入围'


class CrmCustomerContact(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='主键id')
    customer_id = models.BigIntegerField(null=True, verbose_name='客户id')
    nick_name = models.CharField(max_length=20, null=True, verbose_name='姓名')
    sex = models.CharField(max_length=2, null=True, verbose_name='性别')
    customer_position = models.CharField(max_length=4, null=True, verbose_name='职位')
    phone_number = models.CharField(max_length=20, null=True, verbose_name='手机')
    office_phone = models.CharField(max_length=20, null=True, verbose_name='办公电话')
    email = models.CharField(max_length=30, null=True, verbose_name='电子邮箱')
    fax = models.CharField(max_length=50, null=True, verbose_name='传真')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, null=True, verbose_name='删除标志')

    class Meta:
        db_table = '客户联系人表'
        verbose_name = '客户联系人'


class CrmProjectContract(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name='项目合同关联id')
    contract_id = models.BigIntegerField(verbose_name='合同id')
    affiliation_module = models.CharField(max_length=4, null=True, verbose_name='所属模块')
    project_id = models.BigIntegerField(verbose_name='项目id')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, null=True, verbose_name='删除标志')

    class Meta:
        db_table = '项目合同关联表'
        verbose_name = '项目合同关联'
        verbose_name_plural = '项目合同关联'


# ==========================================
# PM Models
# ==========================================

class PmAppraisalProject(models.Model):
    id = models.BigAutoField(primary_key=True)
    code_head = models.CharField(max_length=1, null=True, verbose_name='编号简称')
    dept_id = models.BigIntegerField(null=True, verbose_name='发起部门')
    is_single = models.CharField(max_length=2, null=True, verbose_name='是否单一项目')
    project_owner_id = models.CharField(max_length=255, null=True, verbose_name='项目归属')
    report_number = models.CharField(max_length=50, null=True, verbose_name='报告编号')
    type = models.CharField(max_length=4, null=True, verbose_name='项目类型')
    name = models.CharField(max_length=255, null=True, verbose_name='项目名称')
    address = models.CharField(max_length=255, null=True, verbose_name='坐落')
    appraisal_purpose = models.CharField(max_length=255, null=True, verbose_name='估价目的')
    area = models.CharField(max_length=255, null=True, verbose_name='所属区域')
    principal = models.CharField(max_length=255, null=True, verbose_name='委托方')
    principal_user = models.CharField(max_length=20, null=True, verbose_name='委托方联系人')
    principal_phone = models.CharField(max_length=20, null=True, verbose_name='委托方联系电话')
    principal_address = models.CharField(max_length=255, null=True, verbose_name='委托方联系地址')
    source_id = models.BigIntegerField(null=True, verbose_name='业务来源')
    source_user = models.CharField(max_length=255, null=True, verbose_name='业务来源联系人')
    source_phone = models.CharField(max_length=20, null=True, verbose_name='业务来源联系电话')
    cooperative_company = models.CharField(max_length=255, null=True, verbose_name='合作单位')
    owner_user = models.BigIntegerField(null=True, verbose_name='项目负责人')
    appraisal_user = models.BigIntegerField(null=True, verbose_name='评估人员')
    sale_user = models.BigIntegerField(null=True, verbose_name='商务负责人')
    sale_support_user = models.BigIntegerField(null=True, verbose_name='商务维护人')
    house_usage = models.CharField(max_length=4, null=True, verbose_name='房产用途')
    house_area = models.FloatField(null=True, verbose_name='房产面积')
    land_usage = models.CharField(max_length=4, null=True, verbose_name='土地用途')
    land_area = models.FloatField(null=True, verbose_name='土地面积')
    seller = models.CharField(max_length=50, null=True, verbose_name='出卖人')
    assignee = models.CharField(max_length=50, null=True, verbose_name='受让人')
    trade_type = models.CharField(max_length=255, null=True, verbose_name='交易类型')
    land_usage_right_type = models.CharField(max_length=255, null=True, verbose_name='土地使用权类型')
    estimated_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='预计收费金额')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    commit_survey_time = models.DateTimeField(null=True, verbose_name='提交勘察时间')
    survey_user = models.BigIntegerField(null=True, verbose_name='现场勘察人')
    survey_time = models.DateTimeField(null=True, verbose_name='现场勘察日期')
    filing_date = models.DateField(null=True, verbose_name='归档日期')
    filing_user = models.BigIntegerField(null=True, verbose_name='归档人员')
    filing_opinion = models.CharField(max_length=255, null=True, verbose_name='归档意见')
    deactivation_date = models.DateField(null=True, verbose_name='注销日期')
    deactivation_reason = models.CharField(max_length=255, null=True, verbose_name='注销原因')
    stamp_date = models.DateField(null=True, verbose_name='盖章完成日期')
    stamp_user = models.BigIntegerField(null=True, verbose_name='盖章人员')
    stamp_report_count = models.CharField(max_length=255, null=True, verbose_name='盖章报告份数')
    filing_commit_date = models.DateField(null=True, verbose_name='提交归档日期')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '估价项目PM表'
        verbose_name = '估价项目(PM)'


class PmAssetProject(models.Model):
    id = models.BigAutoField(primary_key=True)
    code_head = models.CharField(max_length=1, null=True, verbose_name='编号简称')
    dept_id = models.BigIntegerField(null=True, verbose_name='发起部门')
    type = models.CharField(max_length=4, null=True, verbose_name='项目类型')
    is_overseas_appraisal = models.CharField(max_length=2, null=True, verbose_name='是否海外资产评估业务')
    is_securities_futures = models.CharField(max_length=2, null=True, verbose_name='是否证券期货评估业务')
    is_state_owned_assets = models.CharField(max_length=2, null=True, verbose_name='是否国有资产评估业务')
    is_financial_assets = models.CharField(max_length=2, null=True, verbose_name='是否金融类资产评估业务')
    is_judicial = models.CharField(max_length=255, null=True, verbose_name='是否司法评估业务')
    source_id = models.BigIntegerField(null=True, verbose_name='业务来源')
    cooperative_company = models.CharField(max_length=255, null=True, verbose_name='合作单位')
    report_number = models.CharField(max_length=50, null=True, verbose_name='报告文号')
    report_name = models.CharField(max_length=255, null=True, verbose_name='报告名称')
    assess_purpose = models.CharField(max_length=4, null=True, verbose_name='评估目的')
    client_name = models.CharField(max_length=255, null=True, verbose_name='委托人名称')
    client_address = models.CharField(max_length=255, null=True, verbose_name='委托人所在地')
    client_nature = models.CharField(max_length=4, null=True, verbose_name='委托人性质')
    assessed_name = models.CharField(max_length=255, null=True, verbose_name='被评估单位名称')
    assessed_address = models.CharField(max_length=255, null=True, verbose_name='被评估单位所在地')
    assessed_object = models.CharField(max_length=4, null=True, verbose_name='评估对象')
    valuation_type = models.CharField(max_length=4, null=True, verbose_name='价值类型')
    assess_user = models.CharField(max_length=255, null=True, verbose_name='评估人员')
    owner_user = models.BigIntegerField(null=True, verbose_name='项目负责人')
    business_user = models.CharField(max_length=255, null=True, verbose_name='业务人员')
    survey_user = models.BigIntegerField(null=True, verbose_name='现场勘察人')
    survey_time = models.DateTimeField(null=True, verbose_name='现场勘察日期')
    asset_service_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='资产评估服务费')
    is_escalate = models.CharField(max_length=2, null=True, verbose_name='是否上报')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    assess_method = models.CharField(max_length=4, null=True, verbose_name='评估方法')
    final_assess_method = models.CharField(max_length=4, null=True, verbose_name='最终选取评估方法')
    assess_conclusion = models.CharField(max_length=255, null=True, verbose_name='评估结论')
    assess_date = models.DateField(null=True, verbose_name='评估基准日')
    assess_report_date = models.DateField(null=True, verbose_name='评估报告日')
    sum_asset_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='总资产账面值')
    sum_debt_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='总负债账面值')
    net_asset_money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='净资产账面值')
    sign_assess_user = models.CharField(max_length=255, null=True, verbose_name='签字估价师')
    stamp_date = models.DateField(null=True, verbose_name='盖章完成日期')
    stamp_user = models.BigIntegerField(null=True, verbose_name='盖章人员')
    stamp_report_count = models.CharField(max_length=255, null=True, verbose_name='盖章报告份数')
    filing_date = models.DateField(null=True, verbose_name='归档日期')
    filing_user = models.BigIntegerField(null=True, verbose_name='归档人员')
    filing_opinion = models.CharField(max_length=255, null=True, verbose_name='归档意见')
    deactivation_date = models.DateField(null=True, verbose_name='注销日期')
    deactivation_reason = models.CharField(max_length=255, null=True, verbose_name='注销原因')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '资产项目管理表'
        verbose_name = '资产项目管理'


class PmPlanSurveyProject(models.Model):
    id = models.BigAutoField(primary_key=True)
    code_head = models.CharField(max_length=1, null=True, verbose_name='编号简称')
    code = models.CharField(max_length=255, null=True, verbose_name='项目编号')
    name = models.CharField(max_length=255, null=True, verbose_name='项目名称')
    is_external = models.CharField(max_length=2, null=True, verbose_name='是否外部合作')
    is_internal_business = models.CharField(max_length=2, null=True, verbose_name='是否内部商务合作')
    is_internal_technology = models.CharField(max_length=2, null=True, verbose_name='是否内部技术合作')
    dept_id = models.BigIntegerField(null=True, verbose_name='发起部门')
    city = models.CharField(max_length=255, null=True, verbose_name='所属城市')
    county = models.CharField(max_length=255, null=True, verbose_name='所属区县')
    first_type = models.BigIntegerField(null=True, verbose_name='一级项目类型')
    second_type = models.BigIntegerField(null=True, verbose_name='二级项目类型')
    third_type = models.BigIntegerField(null=True, verbose_name='三级项目类型')
    first_type_name = models.BigIntegerField(null=True, verbose_name='一级项目类型名称')
    second_type_name = models.BigIntegerField(null=True, verbose_name='二级项目类型名称')
    third_type_name = models.BigIntegerField(null=True, verbose_name='三级项目类型名称')
    principal = models.CharField(max_length=255, null=True, verbose_name='委托方')
    principal_user = models.CharField(max_length=20, null=True, verbose_name='委托方联系人')
    principal_phone = models.CharField(max_length=20, null=True, verbose_name='委托方联系电话')
    principal_email = models.CharField(max_length=255, null=True, verbose_name='委托方邮箱')
    principal_address = models.CharField(max_length=255, null=True, verbose_name='委托方联系地址')
    source_id = models.BigIntegerField(null=True, verbose_name='业务来源')
    source_user = models.CharField(max_length=255, null=True, verbose_name='业务来源联系人')
    source_phone = models.CharField(max_length=20, null=True, verbose_name='业务来源联系电话')
    cooperative_company = models.CharField(max_length=255, null=True, verbose_name='合作单位')
    owner_user = models.BigIntegerField(null=True, verbose_name='项目负责人')
    sale_user = models.BigIntegerField(null=True, verbose_name='商务来源人')
    sale_support_user = models.BigIntegerField(null=True, verbose_name='商务维护人')
    group_user = models.CharField(max_length=255, null=True, verbose_name='项目组成员')
    money = models.DecimalField(max_digits=18, decimal_places=2, null=True, verbose_name='项目金额')
    remark = models.CharField(max_length=255, null=True, verbose_name='备注')
    filing_commit_date = models.DateField(null=True, verbose_name='提交归档日期')
    filing_commit_user = models.BigIntegerField(null=True, verbose_name='归档提交人')
    confirm_filing_date = models.DateField(null=True, verbose_name='确认归档日期')
    confirm_filing_user = models.BigIntegerField(null=True, verbose_name='归档确认人')
    filing_opinion = models.CharField(max_length=255, null=True, verbose_name='归档意见')
    deactivation_date = models.DateField(null=True, verbose_name='注销日期')
    deactivation_reason = models.CharField(max_length=255, null=True, verbose_name='注销原因')
    create_user_id = models.BigIntegerField(null=True, verbose_name='创建人')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_user_id = models.BigIntegerField(null=True, verbose_name='更新人')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    del_flag = models.IntegerField(default=0, verbose_name='删除标志')

    class Meta:
        db_table = '规划勘察项目表'
        verbose_name = '规划勘察项目'
